var bearcat = require('bearcat');
var Sequelize = require('sequelize');
var _ = require('lodash');

var DBMgr = function() {
    this.$id = 'DBMgr';
    this.$lazy = true;
    this.$init = 'init';
    this.$order = 1;
    this.databases = {};
    this._db_config = '${db_config}';
  };

  //导入model--平台
var loadDBGamePlatformModels = function(sequelize) {
    var path = process.cwd() + '/app/dao/db_game_platform/';
    // sequelize.import(path + 'adminUserModel');
    sequelize.import(path + 'proxyBannerModel');
    
    //数据库中没有proxy_banner，将创建proxy_banner表
    sequelize.model('proxy_banner').sync({
      force: false
    });
  
    // //一对一关系定义
    // //在admin_user对象中包含role_config对象，通过外键roleType
    // sequelize.model('admin_user').belongsTo(sequelize.model('role_config'), {
    //   foreignKey: 'roleType',
    //   targetKey: 'roleType'
    // });

  };

  DBMgr.prototype.getDataBase = function(db_name) {
    //Note: 不要直接使用this.sequelize;
    if (!db_name) {
      db_name = 'db_game_platform';
    }
    return this.databases[db_name];
  };

    //默认使用db_game_platform
    DBMgr.prototype.getModel = function(db_name, name) {
    if (arguments.length === 1) {
      name = db_name;
      db_name = 'db_game_platform';
    }
    return this.databases[db_name].model(name);
  };
  

  DBMgr.prototype.loadDataBase = function(name, loadModels, callback) {
    var self = this;
    var db_config = self._db_config[name];
    console.log(db_config);
    if (!db_config) {
      console.error('!!! db config not found: ' + name);
    }
  
    //连接数据库 db_game_platform
    var sequelize = new Sequelize(db_config.connect_string, db_config.config);
  
    return sequelize.authenticate()
      .then(function() {
        //Notice: 数据库操作必须在连接成功之后执行
        loadModels(sequelize);
        self.databases[name] = sequelize;
        if(!!self.onConnected){
          self.onConnected(name);
        }
          if(!!callback) callback();
      }, function(err){
        console.log('Unable to connect to the database:', err);
      });
  };
  
  DBMgr.prototype.setOnDBConnected = function(onConnected){
    this.onConnected = onConnected;
  };
  
DBMgr.prototype.init = function() {
    // console.log(11111111111111,this._db_config);
    var self = this;
    //连接数据库 db_game_platform
    self.loadDataBase('db_game_platform', loadDBGamePlatformModels)
      .then(function(data) {
        console.log(1111111111111);
        // //连接数据库 db_game_main
        // return self.loadDataBase('db_game_main', loadDBGameMainModels);
      })
      .then(function(data) {
        // //测试
        // self.getProxyBanner();
      })
      .catch(function(e) {
        console.error(e);
      });
  };

//   DBMgr.prototype.getProxyBanner = function() {
//     var proxyBannerModel = bearcat.getBean('DBMgr').getModel('db_game_platform', 'proxy_banner');
//     console.log('############ ceshi');
//     proxyBannerModel.findAll({})
//       .then(function (rows) {
//         var obj = {};
//         _.forEach(rows, function(row) {
//           var _row = row.toJSON();
//           var id = _row.id;
//           obj[id] = _row;
//         });
//         console.log('############ proxyBannerModel',obj);
//       })
//       .catch(function (e) {
//         console.error(e);
//       });
//   };

bearcat.module(DBMgr, typeof module !== 'undefined' ? module : {});